package com.astn.bks;

import java.util.HashMap;
import java.util.Map;

import com.astn.bks.util.WebserviceUtils;

import adstn.dialoglib.Dialog;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class BksMain extends Activity {
	/** Called when the activity is first created. */
	private Button btnLogin;
	private Button btnExit;
	private EditText txtUsername;
	private EditText txtPassword;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);
		WebserviceUtils.initial("http://10.0.2.2/quangnd25/index.php");
		btnExit = (Button) findViewById(R.id.btnExit);
		btnExit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// adstn.dialoglib.Dialog.showAlert(BksMain.this,
				// "An error occured, request post fail.");
				finish();
			}
		});
		btnLogin = (Button) findViewById(R.id.btnLogin);
		txtUsername = (EditText) findViewById(R.id.txtUserName);
		txtPassword = (EditText) findViewById(R.id.txtPassword);
		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Map<String, Object> mapPara = new HashMap<String, Object>();
				mapPara.put("USERNAME", txtUsername.getText().toString());
				mapPara.put("PASSWORD", txtPassword.getText().toString());
				if (mapPara.get("USERNAME").toString().length() <= 0) {
					Dialog.showToast(getApplicationContext(),
							"Enter username and password please");
					return;
				}
				WebserviceUtils.setLoginInfo(txtUsername.getText().toString(),
						txtPassword.getText().toString());
				boolean success = WebserviceUtils.login("com_xmlrpc", "login");
				if (success) {
					Intent t = new Intent(BksMain.this, Home.class);
					startActivity(t);
				} else {
					Toast.makeText(getApplicationContext(), "Login fail",
							Toast.LENGTH_LONG).show();
				}

			}

		});
	}

}